﻿using Microsoft.Extensions.DependencyInjection;
using Service.CertWorkFlow;
using Service.Model;
using Service.Utity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkflowCore.Interface;

namespace CertApply
{
    public partial class frmAutomaticHandler : Form
    {
        private readonly AutomaticConfig automaticConfig;
        private RSA rsa;
        public frmAutomaticHandler(string filePath)
        {
            InitializeComponent();
            var rsaInfo = RSAStore.GetRSA();
            if (rsaInfo.message.Length > 0)
            {
                MessageBox.Show(rsaInfo.message);
                Environment.Exit(0);
            }
            rsa = rsaInfo.Result;
            automaticConfig = ConfigHelper.GetConfig(rsa, filePath);
            
        }
        void sendMsg(string msg)
        {
            richTextBoxLog.Invoke(() =>
            {
                richTextBoxLog.AppendText($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} {msg} \r\n");
            });
        }
        private async void frmAutomaticHandler_Load(object sender, EventArgs e)
        {
            var host = Program.ServiceProvider.GetService<IWorkflowHost>();
            host.Start();
            var data = new WorkflowData()
            {
                Action = sendMsg,
                CertResult = null,
                CertService = new Service.CertService(),
                Dns01ChallengeValidationDetails = null,
                Config = automaticConfig
            };
            await host.StartWorkflow("CertWorkFlow", data);
        }
    }
}
